﻿using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IRoleService;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Domain.DomainService.System.RoleService
{
    public class RoleProfileService : IRoleProfileService
    {
        private readonly IRepository<AppRole> _RoleRep;

        public RoleProfileService(IRepository<AppRole> roleRep)
        {
            _RoleRep = roleRep;
        }

        public async Task<DomainResult<AppRole>> CreateRole(string roleName, string Description)
        {
            roleName = roleName.Trim();
            Description = Description.Trim();
            var role = _RoleRep.Table.FirstOrDefault(item => item.RoleName == roleName);

            if (role != null) {
                
                return DomainResult<AppRole>.Error("角色已存在");
             }
            var appRole = new AppRole {Id=Guid.NewGuid(), RoleName = roleName, Description = Description };
            await _RoleRep.CreateAsync(appRole);

            return DomainResult<AppRole>.Success(appRole);

        }

        public async Task<DomainResult<AppRole>> GetRoleById(Guid roleId)
        {
            var role = await _RoleRep.GetByIdAsync(roleId);
            if (role == null)
            {
                return DomainResult<AppRole>.Error("角色不存在");
            }
            if (role.IsDeleted == true)
            {
                return DomainResult<AppRole>.Error("角色已被删除");
            }
            return DomainResult<AppRole>.Success(role);
        }
        //获取所有角色（分页获取）
        public async Task<DomainResult<List<AppRole>>> GetRolesWithPagination(int page, int pageSize)
        {
            var userList = await _RoleRep.GetListAsync();

            if (userList.Count == 0)
            {
                return DomainResult<List<AppRole>>.Error("当前还没有任何角色");
            }
            var userListPage = userList.Skip((page - 1) * pageSize).Take(pageSize).Where(item => item.IsDeleted == false).ToList();

            return DomainResult<List<AppRole>>.Success(userListPage);
        }
        //删除角色
        public async Task<DomainResult<AppRole>> RemoveRole(Guid roleId)
        {
            var role = await _RoleRep.GetByIdAsync(roleId);
            if (role == null)
            {
                return DomainResult<AppRole>.Error("角色不存在");
            }
            if (role.IsDeleted == true)
            {
                return DomainResult<AppRole>.Error("角色已被删除");
            }
            await _RoleRep.DeleteAsync(roleId);
            return DomainResult<AppRole>.Success(role);
        }
        //修改角色信息
        public async Task<DomainResult<AppRole>> UpdateRoleInfo(Guid roleId, AppRole appRole)
        {
            var role = await _RoleRep.GetByIdAsync(roleId);
            if (role == null)
            {
                return DomainResult<AppRole>.Error("用户不存在");
            }

            role.RoleName = appRole.RoleName;
            role.Description = appRole.Description;

            await _RoleRep.UpdateAsync(role);
            return DomainResult<AppRole>.Success(role);
        }
    }
}
